package com.study.sgu.hsp.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author study
 * @version 1.0
 * @date 2020/9/27 23:18
 */
public class NioClient {
    public static void main(String[] args) throws IOException {
        //得到一个网络通道
        SocketChannel socketChannel = SocketChannel.open();
        //设置非阻塞模式
        socketChannel.configureBlocking(false);

        //提供服务器端的IP 端口
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 6666);
        //连接服务器
        if (!socketChannel.connect(inetSocketAddress)) {
            while (!socketChannel.finishConnect()) {
                System.out.println("连接需要时间，客户端不会阻塞");
            }
        }
        //连接成功发送数据
        String str = "hello China";
        ByteBuffer wrap = ByteBuffer.wrap(str.getBytes());

        //发送数据
        socketChannel.write(wrap);
        System.in.read();
    }
}
